% solve for the level of free childcare

% save full income
W0=W;

% first save the baseline outcomes
load ../temp/insample_baseline.txt
min_Y_c=zeros(N,1);

if Cobb_Douglas
    tech='_Cobb_Douglas';
    
    clear table
    tab0=readtable('../temp/share_model.csv','ReadVariableNames',false);
    tab=table2array(tab0);
    share_model=zeros(2,2,3);
    share_model(1,:,1)=tab(1,:);
    share_model(1,:,3)=tab(2,:);
    share_model(2,:,1)=tab(3,:);
    share_model(2,:,2)=tab(4,:);
    share_model(2,:,3)=tab(5,:);

    for m=1:2
        for e=1:2

            eval(['a_m(ind' int2str(m) int2str(e) ')=share_model(m,e,1);']);        
            eval(['a_g(ind' int2str(m) int2str(e) ')=share_model(m,e,3);']);

            if (m==2)
                eval(['a_f(ind' int2str(m) int2str(e) ')=share_model(m,e,2);']);        
            else
                eval(['a_f(ind' int2str(m) int2str(e) ')=0;']);
            end
        end
    end
    
    a_y=1-a_m-a_f-a_g;
else
    tech='';
end

evaluate;
csvwrite(['../temp/outcome_no_free_childcare' tech '.csv'],outcome)
csvwrite(['../temp/doutcome_no_free_childcare' tech '.csv'],doutcome)

% start solving
param_min_Y_c=ones(2,2);

options = optimset('MaxIter',10000,'Display','iter','PlotFcns',@optimplotfval);

x0=log([30 100]*52);

[x,fval,exitflag,output]=fminunc(@distance_free_childcare,x0,options);

y=distance_free_childcare(x);

if (no_response)
    name='_no_response';
else
    name='';
end

csvwrite(['../temp/outcome_free_childcare' name tech '.csv'],outcome)
csvwrite(['../temp/doutcome_free_childcare' name tech '.csv'],doutcome)
